#include "TileManager.h"
#include "windows.h"
#include "Entity.h"
#include "Game.h"
#include "../GameplayStates/GameplayState.h"

CTileManager::CTileManager(void)
{
	m_nScreenWidth	= CGame::GetInstance()->GetWidth();
	m_nScreenHeight = CGame::GetInstance()->GetHeight();
	m_pD3D			= CSGD_Direct3D::GetInstance();
	m_pTM			= CSGD_TextureManager::GetInstance();

	m_nTileWidth	= -1;
	m_nTileHeight	= -1;
	m_nImageID		= -1;
	m_nSizeX		= -1;
	m_nSizeY		= -1;
	m_nTilePixelWidth = -1;
	m_nTilePixelHeight = -1;
}

CTileManager::~CTileManager(void) { }

bool CTileManager::LoadTile(vector<CTile> &vTiles, vector<CTile>& vCollision, vector<CTile>& vEvents, const char* szFilename)
{
	TiXmlDocument doc;
	if (!doc.LoadFile( szFilename ))
		return false;
	TiXmlElement* pRoot = doc.RootElement();
	if (pRoot == nullptr)
		return false;
	vTiles.clear();

	TiXmlElement* pLevel = pRoot->FirstChildElement( "LevelInfo" );
		//TiXmlNode* pLevel = nLevel->FirstChildElement("LevelInfo");

		pLevel->Attribute("tilePixelHeight", (int*)&m_nTilePixelHeight);	
		pLevel->Attribute("tilePixelWidth", (int*)&m_nTilePixelWidth);
		pLevel->Attribute("tileHeight",(int*)&m_nTileHeight);
		pLevel->Attribute("tileWidth", (int*)&m_nTileWidth);

		string fileName = pLevel->Attribute("source_file_name");
		string directory = "../resources/graphics/";
		directory += fileName;
		size_t reqSize = 0;
		mbstowcs_s(&reqSize, NULL, 0, directory.c_str(), 0);
		wchar_t* buffer = new wchar_t[reqSize +1];
		mbstowcs_s(&reqSize, buffer, reqSize+1, directory.c_str(), reqSize);

		m_nImageID = m_pTM->LoadTexture(buffer);
		delete buffer;
	TiXmlElement* nMapdata = pLevel->NextSiblingElement("mapData");
	TiXmlElement* nOverMapData = nMapdata->FirstChildElement("overMapData");
	TiXmlElement* pTile = nOverMapData->FirstChildElement( "TileInfo" );

	while (pTile != nullptr)
	{
		CTile info;
		//save my imageid as an image load into the text manager to get the id.(#)

		int pPosX = info.GetTilePosX();
		pTile->Attribute("posX", &pPosX);
		info.SetPosX(pPosX);

		int pPosY = info.GetTilePosY();
		pTile->Attribute("posY", &pPosY);
		info.SetPosY(pPosY);

		int pSRect_left = info.GetSRectLeft();
		pTile->Attribute("source_left", &pSRect_left);
		info.SetSRectLeft(pSRect_left);
		
		int pSRect_top = info.GetSRectTop();
		pTile->Attribute("source_top", &pSRect_top);
		info.SetSRectTop(pSRect_top);

		int pSRect_right = info.GetSRectRight();
		pTile->Attribute("source_right", &pSRect_right);
		info.SetSRectRight(pSRect_right);

		int pSRect_bot = info.GetSRectBottom();
		pTile->Attribute("source_bot", &pSRect_bot);
		info.SetSRectBottom(pSRect_bot);

		const char* pNameID;//= info->GetNameID();
		pNameID = pTile->Attribute("name");
		//pNameID = pTile->GetText();
		info.SetNameID(pNameID);

		vTiles.push_back(info);		//apply read info to the vector
		pTile = pTile->NextSiblingElement("TileInfo");	//go to the next vector
	}

	TiXmlElement* nOverMapDataCollision = nOverMapData->NextSiblingElement("overMapDataCollision");
	TiXmlElement* pCollisionTile = nOverMapDataCollision->FirstChildElement( "CollisionInfo" );

	while (pCollisionTile != nullptr)
	{
		CTile info;

		int pPosX = info.GetTilePosX();
		pCollisionTile->Attribute("posX", &pPosX);
		info.SetPosX(pPosX);

		int pPosY = info.GetTilePosY();
		pCollisionTile->Attribute("posY", &pPosY);
		info.SetPosY(pPosY);


		int pCRect_left = info.GetCRectLeft();
		pCollisionTile->Attribute("collision_left", &pCRect_left);
		info.SetCRectLeft(pCRect_left);

		int pCTRect_top = info.GetCRectTop();
		pCollisionTile->Attribute("collision_top", &pCTRect_top);
		info.SetCRectTop(pCTRect_top);

		int pCTRect_right = info.GetCRectRight();
		pCollisionTile->Attribute("collision_right", &pCTRect_right);
		info.SetCRectRight(pCTRect_right);

		int pCTRect_bot = info.GetCRectBottom();
		pCollisionTile->Attribute("collision_bot", &pCTRect_bot);
		info.SetCRectBottom(pCTRect_bot);

		

		string pNameID = info.GetNameID();
		pNameID = pCollisionTile->Attribute("name");
		info.SetNameID(pNameID);

		int pCollision = info.GetCollision();
		pCollisionTile->Attribute("bCollision", &pCollision);

		if(pCollision == 1)
			info.SetCollision(true);

		if(info.GetCollision() == true)
		{
			
			vCollision.push_back(info);		//apply read info to the vector
		}
			pCollisionTile = pCollisionTile->NextSiblingElement("CollisionInfo");
	}

	TiXmlNode* nOverMapDataEvent = nOverMapDataCollision->NextSiblingElement("overMapDataEvent");

	TiXmlElement* pEventTile = nOverMapDataEvent->FirstChildElement( "EventInfo" );

	while (pEventTile != nullptr)
	{
		CTile info;

		int pPosX = info.GetTilePosX();
		pEventTile->Attribute("posX", &pPosX);
		info.SetPosX(pPosX);

		int pPosY = info.GetTilePosY();
		pEventTile->Attribute("posY", &pPosY);
		info.SetPosY(pPosY);

		string pEvent = info.GetTileEvent();
		pEvent = pEventTile->Attribute("event");
		info.SetTileEvent(pEvent);

		string pNameID = info.GetNameID();
		pNameID = pEventTile->Attribute("name");
		info.SetNameID(pNameID);

		if(pEvent != "")
		{
			info.SetCRectLeft(pPosX * m_nTileWidth);
			info.SetCRectTop(pPosY * m_nTileHeight);
			info.SetCRectRight(info.GetCRectLeft() + m_nTilePixelWidth);
			info.SetCRectBottom(info.GetCRectTop() + m_nTilePixelHeight);
			info.SetType(OBJ_EVENT);
		}

		if(strcmp(info.GetTileEvent().c_str(), "") != 0)
			vEvents.push_back(info);		//apply read info to the vector
		pEventTile = pEventTile->NextSiblingElement("EventInfo");
	}
	return true;
}

void CTileManager::Render(vector<CTile>const & vTiles)
{
	for (unsigned int i = 0; i < vTiles.size(); i++)
	{
		//RECT rTextureIDBot = vTiles[i].GetSRectBottom);	//source RECT... THIS NEEDS An IMAGE ID COUNT... MAYBE
		//RECT rDestID = CellDestinationAlgorithm(vTiles[i]);		//Destination RECT


		RECT rSource = { 0,0,0,0 };

		rSource.bottom = vTiles[i].GetSRectBottom();
		rSource.top = vTiles[i].GetSRectTop();
		rSource.left = vTiles[i].GetSRectLeft();
		rSource.right = vTiles[i].GetSRectRight();

		/*if (vTiles[i].GetTilePosX() * GetTilePixelWidth() + GetTilePixelWidth() > m_nScreenWidth  - 64 || vTiles[i].GetTilePosX() * GetTilePixelWidth() < 0 + 64
			|| vTiles[i].GetTilePosY() * GetTilePixelHeight() + GetTilePixelHeight() > m_nScreenHeight  - 64 || vTiles[i].GetTilePosY() * GetTilePixelHeight() < 0 + 64)
		{
			continue;
		}*/
		//else 
		//{
			m_pTM->Draw(GetImageID(), vTiles[i].GetTilePosX() * GetTilePixelWidth() - CGameplayState::GetInstance()->GetCamera().camPosX, vTiles[i].GetTilePosY() * GetTilePixelHeight(), 1.0f, 1.0f, &rSource, 0,0,0,D3DCOLOR_XRGB(255,255,255));
		//}
	}
}